#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
# This source file is part of the Cangjie Project, licensed under Apache-2.0
# with Runtime Library Exception.
#
# See https://cangjie-lang.cn/pages/LICENSE for license information.

# The Cangjie API is in Beta. For details on its capabilities and limitations, please refer to the README file.

import os
import subprocess
import sys
import shutil
import platform
import argparse

# Check CANGJIE_HOME
if not os.environ.get("CANGJIE_HOME"):
    print("error: cannot find CANGJIE_HOME, please make sure cangjie sdk is configured", file=sys.stderr)
    sys.exit(1)

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))

def check_call(command):
    try:
        return subprocess.check_call(command)
    except subprocess.CalledProcessError as e:
        print(f"Command '{e.cmd}' returned non-zero exit status {e.returncode}.")
        sys.exit(e.returncode) 

def read_cj_files(target_dir=None):
    """
    读取指定目录下以.cj结尾的文件
    :param target_dir: 目标目录，默认当前目录
    """
    # 默认使用当前工作目录
    if target_dir is None:
        target_dir = os.getcwd()
    
    print(f"正在读取目录 [{target_dir}] 下以.cj结尾的文件：")
    print("=" * 50)
    
    # 遍历目录（含子目录）
    for root, dirs, files in os.walk(target_dir):
        for file in files:
            if file.endswith(".cj"):
                # 输出文件完整路径
                full_path = os.path.join(root, file)
                print(full_path)
                check_call(['cjc', full_path, '--output-type=staticlib'])
                

if __name__ == "__main__":
    # 如需指定目录，修改为 read_cj_files("D:/test") 或 "/home/test"
    read_cj_files(CURRENT_DIR  + "/expected/c_module")